<?php
/*
 * @category   Laurent
 * @package    Laurent_OrderTickets
 * @copyright  Copyright (c) 2011 Laurent Clouet
 * @license    http://opensource.org/licenses/afl-3.0.php  Academic Free License (AFL 3.0)
 * @author     Laurent Clouet <laurent35240@gmail.com>
 *
 */
?>
<?php
/** @var $this Laurent_OrderTickets_Block_Adminhtml_Chat_View_Tab_Tickets */
/** @var $session Mage_Admin_Model_Session */
$session = Mage::getSingleton('admin/session');
/** @var $coreHelper Mage_Core_Helper_Data */
$coreHelper = Mage::helper('core');
/** @var $orderTicketsHelper Laurent_OrderTickets_Helper_Data */
$orderTicketsHelper = Mage::helper('ordertickets');
?>
<?php foreach ($this->getTickets() as $ticket): ?>
    <?php /** @var $ticket Laurent_OrderTickets_Model_Ticket */?>
    <div class="entry-edit">
        <div class="entry-edit-head">
            <h4>
                <?php if ($ticket->isRequest()): ?>
                    <?php echo $this->__('Message from %s', $this->getChat()->getCustomerFirstname() . ' ' . $this->getChat()->getCustomerLastname()) ?>
                <?php else: ?>
                    <?php echo $this->__('Answer') ?>
                <?php endif; ?>
            </h4>
            <?php if($session->isAllowed('sales/ordertickets/delete')): ?>
                <div class="tools">
                    <a href="<?php echo $this->getUrl('*/*/deleteTicket', array('ticket_id' => $ticket->getId())) ?>" 
                    onclick="return confirm('<?php echo $this->__('Are you sure you want to delete this ticket?') ?>');">
                        <?php echo $this->__('Delete') ?>
                    </a>
                </div>
            <?php endif; ?>
        </div>
        <div class="fieldset fieldset-wide">
            <table class="form-list">
                <tbody>
                    <tr>
                        <td class="label">
                            <label>
                                <?php echo $this->__('Send the') ?>
                            </label>
                        </td>
                        <td class="value">
                            <?php echo $coreHelper->formatDate($ticket->getCreatedAt(), 'full', true) ?>
                        </td>
                    </tr>
                    <tr>
                        <td class="label">
                            <label>
                                <?php echo $this->__('Message') ?>
                            </label>
                        </td>
                        <td class="value">
                            <?php echo nl2br(htmlspecialchars($ticket->getMessage())) ?>
                        </td>
                    </tr>
                </tbody>
            </table>
        </div>
    </div>
<?php endforeach; ?>
<?php if($this->getChat()->canReply()): ?>
<div class="entry-edit">
    <div class="entry-edit-head">
        <h4><?php echo $this->__('Add an answer'); ?></h4>
    </div>
    <div class="fieldset fieldset-wide">
        <table class="form-list">
            <tr>
                <td class="label"><label for="message"><?php echo $this->__('Message') ?></label></td>
                <td class="value"><textarea name="answer[message]" id="message"></textarea></td>
            </tr>
            <tr>
                <td class="label"><label for="status"><?php echo $this->__('Status') ?></label></td>
                <td class="value">
                    <select name="answer[status]" id="status">
                        <?php foreach ($orderTicketsHelper->getChatStatuses() as $statusKey => $statusLabel): ?>
                            <option value="<?php echo $statusKey ?>" <?php if ($statusKey == $this->getChat()->getStatus()): ?>selected="selected"<?php endif; ?>>
                                <?php echo $statusLabel ?>
                            </option>
                        <?php endforeach; ?>
                    </select>
                </td>
            </tr>
            <tr>
                <td class="label"></td>
                <td class="value f-right">
                    <input type="hidden" name="answer[chat_id]" value="<?php echo $this->getChat()->getId() ?>" />
                    <button onclick="postAnswer();" class="form-button" type="submit"><span><?php echo $this->__('Send') ?></span></button>
                </td>
            </tr>
        </table>
    </div>
</div>
<?php endif; ?>

<script type="text/javascript">
    function postAnswer()
    {
        var form = document.getElementById("edit_form");
        form.action = '<?php echo $this->getPostAnswerUrl() ?>';
        form.submit();
    }
</script>